﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.Personal
{
    public partial class AddressBookChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        #endregion

        #region 读取数据


        private void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                AddressBook Tm = SDDbSession.Default.From<AddressBook>().Where(AddressBook._.AddrID == id).ToFirst();
                txt_Name.Text = Tm.Name;
                txt_Addr.Text = Tm.Address;
                txt_company.Text = Tm.Company;
                txt_email.Text = Tm.Email;
                txt_fax.Text = Tm.Cell;
                txt_phone.Text = Tm.Phone;
                drp_sex.SelectedValue = Convert.ToBoolean(Tm.Sex) ? "True" : "False";
                drp_status.SelectedValue = Tm.StatusID.ToString();
            }
        }
        #endregion

        #region 保存数据
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            AddressBook Tm = new AddressBook();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Tm.AddrID = int.Parse(Request["id"]);
                Tm.Attach();
                Tm.Name = txt_Name.Text;
                Tm.Address = txt_Addr.Text;
                Tm.Cell = txt_fax.Text;
                Tm.Company = txt_company.Text;
                Tm.Email = txt_email.Text;
                Tm.Name = txt_Name.Text;
                Tm.Phone = txt_phone.Text;
                Tm.Sex = drp_sex.SelectedValue == "True" ? true : false;
                Tm.StatusID = int.Parse(drp_status.SelectedValue);
                returnvalue = SDDbSession.Default.Update<AddressBook>(Tm);
            }
            else
            {
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                Tm.DeAttach();
                Tm.Name = txt_Name.Text;
                Tm.Address = txt_Addr.Text;
                Tm.Cell = txt_fax.Text;
                Tm.Company = txt_company.Text;
                Tm.Email = txt_email.Text;
                Tm.Name = txt_Name.Text;
                Tm.Phone = txt_phone.Text;
                Tm.Sex = drp_sex.SelectedValue == "True" ? true : false;
                Tm.StatusID = int.Parse(drp_status.SelectedValue);
                Tm.CreateDate = DateTime.Now;
                Tm.CreateUserID = user.UserID;
                returnvalue = SDDbSession.Default.Insert<AddressBook>(Tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        #endregion
    }
}